package com.offcn.dao.Impl;

import com.offcn.bean.Course;
import com.offcn.bean.User;
import com.offcn.dao.CourseDao;
import com.offcn.utils.BaseDao;
import com.offcn.utils.Page;

import java.util.List;

/**
 * @Author: wy
 * @Date: 2022/9/12 15:25
 */
public class CourseDaoImpl extends BaseDao<Course> implements CourseDao {
    @Override
    public Page<Course> getCourseListPage(int pno, int psize, String cname) {
        Page<Course> coursePage = new Page<>();
        coursePage.setPageNo(pno);
        coursePage.setPageSize(psize);
        int courseCount = getCourseCount(cname);
        coursePage.setTotalRecord(courseCount);

        String  sql="select * from course where courseName like ? limit ?,?";
        List<Course> beanList = getBeanList(sql, "%" + cname + "%",
                (coursePage.getPageNo() - 1) * coursePage.getPageSize(), coursePage.getPageSize());
        coursePage.setList(beanList);
        return coursePage;
    }

    @Override
    public int getCourseCount(String cname) {
        String sql="SELECT count(*) from course where  courseName like ?";
        Long singleValue = (Long) getSingleValue(sql, "%" + cname + "%");
        return singleValue.intValue();
    }

    public static void main(String[] args) {
        CourseDao  courseDao=new CourseDaoImpl();
        Page<Course> j = courseDao.getCourseListPage(1, 8, "");
        for (Course course : j.getList()) {
            System.out.println(course);
        }
    }
}
